import { http } from '@/utils/http'

// 获取验证码
export const getCaptcha = () => {
  const uuid = uni.getStorageSync('__DC_STAT_UUID')
  return uni.request({
    method: 'GET',
    url: '/user/images/captcha',
    data: { uuid },
    header: {
      'Content-Type': 'application/json',
      Accept: 'image/jpeg, image/png, image/gif',
    },
    responseType: 'arraybuffer', // 使用 arraybuffer 接收二进制数据
  })
}

// 登录接口
export const login = (data: {
  username: string
  password: string
  verifyCode: string
  uuid: string
}) => {
  return uni.request({
    method: 'POST',
    url: '/user/login',
    data,
    header: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  })
}

// 退出登录
export const logout = () => {
  const token = uni.getStorageSync('token')
  return uni.request({
    url: '/user/logout',
    method: 'GET',
    header: {
      token: token,
    },
  })
}

// 检查登录状态
export const checkLogin = () => {
  const token = uni.getStorageSync('token')
  return uni.request({
    url: '/user/checklogin',
    method: 'POST',
    header: {
      token: token,
    },
  })
}

// 获取当前用户信息
export const getCurrentUser = () => {
  const token = uni.getStorageSync('token')
  return uni.request({
    url: '/user/getCurrentUser',
    method: 'GET',
    header: {
      token: token,
    },
  })
}

// 获取待审核内容
export const findAllDeniedPosts = () => {
  const token = uni.getStorageSync('token')
  return uni.request({
    url: '/api/post/queryfou',
    method: 'POST',
    header: {
      'Content-Type': 'application/json',
      token: token,
    },
  })
}

// 审核通过内容
export const approvePost = (id: number) => {
  const token = uni.getStorageSync('token')
  return uni.request({
    url: `/api/post/updatestatue/${id}`,
    method: 'POST',
    header: {
      'Content-Type': 'application/json',
      token: token,
    },
  })
}

// 拒绝帖子
export const rejectPost = (id: number) => {
  const token = uni.getStorageSync('token')
  return uni.request({
    url: `/api/post/updatestatueislike/${id}`,
    method: 'POST',
    header: {
      'Content-Type': 'application/json',
      token: token,
    },
  })
}

// 获取已拒绝的帖子
export const findRejectedPosts = () => {
  const token = uni.getStorageSync('token')
  return uni.request({
    url: '/api/post/findAlldefine',
    method: 'POST',
    header: {
      'Content-Type': 'application/json',
      token: token,
    },
  })
}

// 更新用户个人信息
export const updateUserProfile = (userProfile: {
  username?: string
  gender?: number
  school?: string
  zhuanye?: string
  avatar?: string
  phone?: string
  unionid?: string
  country?: string
  province?: string
  city?: string
  language?: string
  wechat?: string
}) => {
  const token = uni.getStorageSync('token')
  return uni.request({
    url: '/user/updateProfile',
    method: 'POST',
    data: userProfile,
    header: {
      'Content-Type': 'application/x-www-form-urlencoded',
      token: token,
    },
  })
}

// 上传头像
export const uploadAvatar = (filePath: string) => {
  const token = uni.getStorageSync('token')
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: '/user/uploadAvatar',
      filePath: filePath,
      name: 'avatar',
      header: {
        token: token,
      },
      success: (res) => {
        if (res.statusCode === 200) {
          const data = JSON.parse(res.data)
          resolve(data)
        } else {
          reject(new Error('上传失败'))
        }
      },
      fail: (err) => {
        reject(err)
      },
    })
  })
}

// 修改密码
export const updatePassword = (data: { id: number; oldpassword: string; password: string }) => {
  const token = uni.getStorageSync('token')
  return uni.request({
    url: '/user/updatepassword',
    method: 'POST',
    data,
    header: {
      'Content-Type': 'application/x-www-form-urlencoded',
      token: token,
    },
  })
}

// 导出其他接口...
